package com.example.wineknower.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttpUtil {
    public static final MediaType STRING = MediaType.parse("application/json; charset=utf-8");
    public static final String BASE_URL = "http://10.0.2.2:8080/";
    private static Map<String, List<Cookie>> cookiestore = new HashMap<String, List<Cookie>>();
    //创建线程池
    private static ExecutorService threadPool = Executors.newFixedThreadPool(30);
    //创建默认的OkHttpClient对象
    private static OkHttpClient okHttpClient =new OkHttpClient.Builder().cookieJar(new CookieJar() {
        @Override
        public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {

        }

        @Override
        public List<Cookie> loadForRequest(HttpUrl url) {
            List<Cookie> cookies = cookiestore.get(url.host());
            return cookies==null?new ArrayList<Cookie>():cookies;
        }
    }).build();
    
    public static String getRequest(final String url) throws Exception {
        FutureTask<String> task = new FutureTask<String>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                Request request = new Request.Builder().url(url).build();
                   Response response = okHttpClient.newCall(request).execute();
                if(response.isSuccessful()&&response.body()!=null){
                    return response.body().string().trim();
                }
                else{
                    return null;
                }
            }
        });
        threadPool.submit(task);
        System.out.print("http返回值："+task.get());
        return task.get();

    }
//    public static String PostRequestjson(final String url, final String json) throws Exception {
//        FutureTask<String> task = new FutureTask<String>(new Callable<String>() {
//            @Override
//            public String call() throws Exception {
//                Request request = new Request.Builder().url(url).post(RequestBody.create(JSON,json)).build();
//                Response response = okHttpClient.newCall(request).execute();
//                if(response.isSuccessful()&&response.body()!=null){
//                    return response.body().string().trim();
//                }
//                else{
//                    return null;
//                }
//            }
//        });
//        threadPool.submit(task);
//        return task.get();
//    }
    public static String PostRequest(final String url, final String prameter) throws Exception {
        FutureTask<String> task = new FutureTask<String>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                RequestBody body = new FormBody.Builder().add("type",prameter).build();
                final Request request = new Request.Builder().url(url).post(body).build();
                Response response = okHttpClient.newCall(request).execute();
                if(response.isSuccessful()&&response.body()!=null){
                    return response.body().string().trim();
                }
                else{
                    return null;
                }
            }
        });
        threadPool.submit(task);
        return task.get();
    }
//    public static String PostRequest(String url,String json) throws Exception {
//        FutureTask<String> task = new FutureTask<String>(()->{
//            Request request = new Request.Builder().url(url).post(RequestBody.create(JSON,json)).build();
//            Response response = okHttpClient.newCall(request).execute();
//            if(response.isSuccessful()&&response.body()!=null){
//                return response.body().string().trim();
//            }
//            else{
//                return null;
//            }
//        });
//        threadPool.submit(task);
//        return task.get();
//    }
}
